package co.chuangdian.oflauncher.widgets;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import cn.onekit.CALLBACK;
import cn.onekit.CONFIG;
import cn.onekit.JSON;
import cn.onekit.MESSAGE;
import co.chuangdian.oflauncher.MainActivity;
import co.chuangdian.oflauncher.R;
import co.chuangdian.oflauncher.widgets.setting.HomeActivity;
import uc.MyRefreshLayout;
import uc.MyScrollView;

public class WidgetsFragment extends Fragment {
	LinearLayout widgetsListView;
	public static MyScrollView scrollView;
	MyRefreshLayout refreshLayout;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.widgets, container, false);
		widgetsListView = (LinearLayout) view.findViewById(R.id.widgetsListView);
		scrollView = (MyScrollView) view.findViewById(R.id.scrollView);
		refreshLayout = (MyRefreshLayout) view.findViewById(R.id.refreshLayout);
		refreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				mHandler.sendEmptyMessage(0);
			}

		});
		return view;
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			startActivity(new Intent(getActivity(), HomeActivity.class));
			getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
			refreshLayout.setRefreshing(false);
		};
	};
	JSONArray widgets = new JSONArray();
	BaseAdapter adapter;

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		MESSAGE.receive("widgets", null, new CALLBACK<Bundle>() {

			@Override
			public void run(boolean isError, Bundle result) {
				reloadData();
			}
		});
		scrollView.parentScrollView = MainActivity.drawerLayout;
		reloadData();

	}

	void reloadData() {
		List<JSONObject> list = new ArrayList<JSONObject>();
		JSONArray allwidgets = JSON.parses(CONFIG.get("widgets"));
		for (int i = 0; i < allwidgets.length(); i++) {
			JSONObject widget = (JSONObject) allwidgets.opt(i);
			if (widget.optBoolean("hidden")) {
				continue;
			} else {
				list.add(widget);
			}
		}
		widgets = new JSONArray(list);

		int childCount = widgetsListView.getChildCount();
		for (int i = 0; i < childCount; i++) {
			widgetsListView.removeViewAt(0);
		}
		addChildView();

	}

	/**
	 * 添加子view
	 */
	void addChildView() {
		for (int i = 0; i < widgets.length(); i++) {
			JSONObject widget = widgets.optJSONObject(i);
			WidgetListRow row = new WidgetListRow(getActivity(), widget);
			row.selectedScrollView = scrollView;
			if (widget.optBoolean("system")) {
				row.setControl(widget);
			} else {
				row.setWidget(widget);
			}
			row.setOnClick(widget);
			widgetsListView.addView(row);
		}
	}
}
